package boogui.seat;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;


public class BooguiSeatDAO implements IBooguiSeatDAO{
	
	private final String DRV = "oracle.jdbc.driver.OracleDriver";
	
	private final String URL = "jdbc:oracle:thin:jwj8508/jwj8508@112.216.253.125:1521:xe";
	
	private String insertSeatSQL = "insert into seatinfo values()";
	
	private String selectSeatSQL = "select * from seatinfo where showno = ? order by showno desc";
	
	
	Connection conn = null;
	PreparedStatement pstmt = null;
	boolean result = false;
	
	
	public Connection openDatabase() {
		try {
			Class.forName(DRV);
			return DriverManager.getConnection(URL);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	
	public void closeDatabase(Connection c, PreparedStatement p, ResultSet r) {
		
		if (c != null){try {c.close(); c = null;} catch (Exception e) {}}
		if (p != null){try {p.close(); p = null;} catch (Exception e) {}}
		if (r != null){try {r.close(); r = null;} catch (Exception e) {}}
		
	}
	
	@Override
	public boolean insertSeat(MemberSeat4 m) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		boolean result = false;
		
		
		try{
			conn = this.openDatabase();
			pstmt = conn.prepareStatement(insertSeatSQL);
			pstmt.executeUpdate();
			result = true;
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			this.closeDatabase(conn, pstmt, null);
		}
		return result;
	}

	@Override
	public List<String> selectSeat(int showno) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<String> result = new ArrayList<>();
		
		try{
			conn = openDatabase();
			pstmt = conn.prepareStatement(selectSeatSQL);
			pstmt.setInt(1, showno);
			rs = pstmt.executeQuery();
			while(rs.next()){
				
				for(int i = 2; i <= 181; i++){
					result.add(rs.getString(i));	
				}
				
			}
			
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			closeDatabase(conn, pstmt, rs);
		}
		
		return result;
	}

}